//
//  EditAfterAddViewController.m
//  QRMaster
//
//  Created by myApple on 15/4/27.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "EditAfterAddViewController.h"
#import "LineView.h"
#import "RoomSeclectViewController.h"
#import "AccessSeclectViewController.h"
#import "RequestNetWork.h"
#import "PromptInfoView.h"
#import <AddressBookUI/AddressBookUI.h>
#import <AddressBook/AddressBook.h>
#import "RequestNetWork.h"
#import "StaffManagerViewController.h"
#import "PromptInfoView.h"
#import "validateInputValue.h"
#import "StaffLogic.h"
#import "AreaChooseController.h"

@interface EditAfterAddViewController ()<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate,ABPeoplePickerNavigationControllerDelegate,UIAlertViewDelegate>
{
    NSUserDefaults * userInfo;
    UIImageView * promtView;//提示操作页面
    
    UIView * editingView;
    UITextField * telePhoneTextField;
    UITextField * userNameTextField;
    UILabel * editingPositonLabel;
    UIView * editingPositonView;
    UIView * editingDutyView;
    UIView * editingRoleView;
    UILabel * editingDutyLabel;
    UILabel * editingRoleLabel;
    UIButton * roomBtn;//开门权限
    UIButton * accessBtn;//管理权限
    
    UIImageView * manView;
    UIButton * manBtn;
    UIImageView * womanView;
    UIButton * womanBtn;
    
    UIButton * mobileBtn;
    UIButton * emailBtn;
    UIButton * mobileHeaderBtn;
    UIButton * addAddressTelePhoneBtn;
    UILabel * emailHeaderLabel;
    UILabel * areaCodeIdLabel;
    UILabel * areaLabel;
    
    UIButton * positionBtn;//点击下拉
    UIButton * dutyBtn;
    UIButton * roleBtn;
    
    NSString * sexFlag;
    UIImageView * titleImgView;//个人头像图
    NSMutableDictionary * saveDict;
    UITableView * listTableView;//职务职责角色的下拉菜单
    
    NSArray * positionArray;
    NSArray * dutyArray;
    NSArray * roleArray;
    
    NSString * positionIndex;
    NSString * dutyIndex;
    NSString * roleIndex;
    UIView * editBackView;
    UIImageView * editImgView;
}

#define fontSize [UIFont systemFontOfSize:14.0]
#define ViewHeigth  50

@end

@implementation EditAfterAddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hiddenKeyboard) name:@"KCHiddenKeyBoardNotification" object:nil];
    sexFlag = @"1";
    saveDict = [[NSMutableDictionary alloc]init];
    roleArray = @[NSLocalizedString(@"general", nil),NSLocalizedString(@"groupManagement", nil)];
    roleIndex = @"3";
    [self prepareView];
    [self prepareEditingView];
    [self prepareListView];
    self.view.backgroundColor = RGBA(237, 238, 241, 1);
    userInfo = [NSUserDefaults standardUserDefaults];
    self.title = NSLocalizedString(@"addStaff", nil);
    if ([[userInfo objectForKey:@"address"]isEqualToString:@"yes"]) {
        [self preparePromtView];
    }
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    editBackView.hidden = NO;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    editBackView.hidden = YES;
}



#pragma mark -- 操作提示页面
-(void)preparePromtView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 0;
    float width = ScreenWidth;
    float height = ScreenHeight;
    
    promtView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    promtView.backgroundColor = RGBA(0, 0, 0, 0.5);
    promtView.userInteractionEnabled = YES;
    [self.view addSubview:promtView];
    UITapGestureRecognizer * promtViewClick = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickPromtView)];
    [promtView addGestureRecognizer:promtViewClick];
    
    x = 0;
    width = ScreenWidth;
    height = 380.0 / 640 * width;
    y = 64;
   UIImageView * addressImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    addressImgView.image = [UIImage imageNamed:NSLocalizedString(@"addStaffPromt", nil)];
    [promtView addSubview:addressImgView];
    
}

-(void)prepareView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float x = ScreenWidth - 50;
    float y = 2;
    float width = 40;
    float height = 40;
    
    editBackView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [self.navigationController.navigationBar addSubview:editBackView];
    x = 10;
    width = 20;
    height = 20;
    y = 10;
    editImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editImgView.image = [UIImage imageNamed:@"ic_save"];
    editImgView.userInteractionEnabled = YES;
    [editBackView addSubview:editImgView];
    UITapGestureRecognizer * editTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(saveClick)];
    [editBackView addGestureRecognizer:editTap];
    
}

-(void)prepareEditingView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = self.view.frame.size.height;
    float x = 0;
    float y = 0;
    float width = ScreenWidth;
    float height = ScreenHeight - y;
    
    editingView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editingView.backgroundColor = RGBA(237, 238, 241, 1);
    [self.view addSubview:editingView];
    
    //类型
    x = 10;
    y = 5;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    UIView * typeView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    typeView.backgroundColor = RGBA(248, 248, 248, 1);
    [editingView addSubview:typeView];
    
    UILabel * typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    typeLabel.text = NSLocalizedString(@"type", nil);
    typeLabel.font = [UIFont systemFontOfSize:14.0];
    [typeView addSubview:typeLabel];
    
    // mobile
    x = 90;
    y = 0;
    width = 40;
    height = 40;
    UIImageView * mobileView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [typeView addSubview:mobileView];
    mobileView.userInteractionEnabled = YES;
    UITapGestureRecognizer * mobileTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(mobileClick)];
    [mobileView addGestureRecognizer:mobileTap];
    
    x = 10;
    y = 20;
    width = 20;
    height = 20;
    mobileBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [mobileBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [mobileBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [mobileBtn addTarget:self action:@selector(mobileClick) forControlEvents:UIControlEventTouchUpInside];
    mobileBtn.selected = YES;
    [mobileView addSubview:mobileBtn];
    
    x = 120;
    y = 20;
    width = 60;
    height = 20;
    UILabel * mobileLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    mobileLabel.text =  NSLocalizedString(@"loginPromt", nil);
    mobileLabel.font = [UIFont systemFontOfSize:14.0];
    [typeView addSubview:mobileLabel];
    
    //email
    x = 180;
    y = 0;
    width = 40;
    height = 40;
    UIImageView * emailImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [typeView addSubview:emailImageView];
    emailImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer * emailTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(emailClick)];
    [emailImageView addGestureRecognizer:emailTap];
    
    x = 10;
    y = 20;
    width = 20;
    height = 20;
    emailBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [emailBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [emailBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [emailBtn addTarget:self action:@selector(emailClick) forControlEvents:UIControlEventTouchUpInside];
    [emailImageView addSubview:emailBtn];
    
    x = 220;
    height = 20;
    y = 20;
    UILabel * emailLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 60, height)];
    emailLabel.text =  NSLocalizedString(@"mineEmail", nil);
    emailLabel.font = [UIFont systemFontOfSize:14.0];
    [typeView addSubview:emailLabel];
    
    
    //手机
    x = 10;
    y = typeView.frame.size.height + typeView.frame.origin.y + 5;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    UIView * telePhoneView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    telePhoneView.backgroundColor = RGBA(248, 248, 248, 1);
    [editingView addSubview:telePhoneView];
    
    x = 100;
    y = 15;
    width = ScreenWidth - x - 60;
    height = 20;
    telePhoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    telePhoneTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    telePhoneTextField.placeholder = NSLocalizedString(@"telePhoneNum", nil);
    telePhoneTextField.font = [UIFont systemFontOfSize:14.0];
    telePhoneTextField.borderStyle = UITextBorderStyleNone;
    telePhoneTextField.delegate = self;
    telePhoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    telePhoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    [telePhoneView addSubview:telePhoneTextField];
    
    //前面点击的区号按钮
    x = 10;
    y = 5;
    width = 40;
    mobileHeaderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    mobileHeaderBtn.frame = CGRectMake(x, y, width, height);
    [mobileHeaderBtn addTarget:self action:@selector(chooseArea) forControlEvents:UIControlEventTouchUpInside];
    [telePhoneView addSubview:mobileHeaderBtn];
    
    areaCodeIdLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 60, 15)];
    areaCodeIdLabel.text = [NSString stringWithFormat:@"+%@",[QRSingleton sharedQRSingleton].currentAreaCodeString];
    areaCodeIdLabel.font = [UIFont systemFontOfSize:10.0];
    [mobileHeaderBtn addSubview:areaCodeIdLabel];
    
    areaLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 15, 60, 15)];
    areaLabel.font = [UIFont systemFontOfSize:10.0];
    areaLabel.text = [QRSingleton sharedQRSingleton].currentAreaString;
    [mobileHeaderBtn addSubview:areaLabel];
    
    emailHeaderLabel = [[UILabel alloc]initWithFrame:CGRectMake(mobileHeaderBtn.frame.origin.x, mobileHeaderBtn.frame.origin.y + 10, mobileHeaderBtn.frame.size.width, mobileHeaderBtn.frame.size.height)];
    emailHeaderLabel.text = NSLocalizedString(@"mineEmail", nil);
    emailHeaderLabel.hidden = YES;
    emailHeaderLabel.font = [UIFont systemFontOfSize:14.0];
    [telePhoneView addSubview:emailHeaderLabel];
    
    x = telePhoneView.frame.size.width - 50;
    y = 0;
    width = 50;
    height = 50;
    addAddressTelePhoneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addAddressTelePhoneBtn.frame = CGRectMake(x, y, width, height);
    [addAddressTelePhoneBtn setBackgroundColor:RGBA(135, 199, 66, 1)];
    [addAddressTelePhoneBtn setBackgroundImage:nil forState:0];
    [addAddressTelePhoneBtn addTarget:self action:@selector(addTelePhone) forControlEvents:UIControlEventTouchUpInside];
    [telePhoneView addSubview:addAddressTelePhoneBtn];
    
    UIImageView * adderssImgView = [[UIImageView alloc]initWithFrame:CGRectMake(10,10, 30, 30)];
    adderssImgView.image = [UIImage imageNamed:@"ic_txl"];
    [addAddressTelePhoneBtn addSubview:adderssImgView];
    
    
    //姓名
    x = 10;
    y = telePhoneView.frame.size.height + telePhoneView.frame.origin.y + 5;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    UIView * nameView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    nameView.backgroundColor = RGBA(248, 248, 248, 1);
    [editingView addSubview:nameView];
    
    x = 10;
    y = 15;
    width = 40;
    height = 20;
    UILabel * editUserNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editUserNameLabel.text = NSLocalizedString(@"userName", nil);
    editUserNameLabel.font = fontSize;
    [nameView addSubview:editUserNameLabel];
    
    x = 100;
    y = 10;
    width = nameView.frame.size.width - x ;
    height = 30;
    userNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, width, height)];
    userNameTextField.font = [UIFont systemFontOfSize:14.0];
    userNameTextField.borderStyle = UITextBorderStyleNone;
    userNameTextField.placeholder = NSLocalizedString(@"inputName", nil);
    userNameTextField.delegate = self;
    [nameView addSubview:userNameTextField];
    
    //性别
    x = 10;
    y = 5 + nameView.frame.size.height + nameView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    UIView * sexView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    sexView.backgroundColor = RGBA(248, 248, 248, 1);
    [editingView addSubview:sexView];
    
    
    x = 10;
    y = 15;
    width = 60;
    height = 20;
    UILabel * editSexLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editSexLabel.text = NSLocalizedString(@"sex", nil);
    editSexLabel.font = fontSize;
    [sexView addSubview:editSexLabel];
    
    x = 100;
    y = 0;
    width = 40;
    height = 50;
    manView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [sexView addSubview:manView];
    manView.userInteractionEnabled = YES;
    UITapGestureRecognizer * manTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(manClick)];
    [manView addGestureRecognizer:manTap];
    
    x = 100;
    y = 15;
    width = 20;
    height = 20;
    manBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [manBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [manBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [manBtn addTarget:self action:@selector(manClick) forControlEvents:UIControlEventTouchUpInside];
    manBtn.selected = YES;
    [sexView addSubview:manBtn];
    
    x = 140;
    height = 20;
    UILabel * manLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 40, height)];
    manLabel.text = NSLocalizedString(@"man", nil);
    manLabel.font = fontSize;
    [sexView addSubview:manLabel];
    
    x = 180;
    y = 0;
    width = 40;
    height = ViewHeigth;
    womanView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [sexView addSubview:womanView];
    womanView.userInteractionEnabled = YES;
    UITapGestureRecognizer * womanTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(womanClick)];
    [womanView addGestureRecognizer:womanTap];
    
    x += 10;
    y += 15;
    width = 20;
    height = 20;
    womanBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [womanBtn setBackgroundImage:[UIImage imageNamed:@"36_1"] forState:0];
    [womanBtn setBackgroundImage:[UIImage imageNamed:@"36_2"] forState:UIControlStateSelected];
    [womanBtn addTarget:self action:@selector(womanClick) forControlEvents:UIControlEventTouchUpInside];
    [sexView addSubview:womanBtn];
    
    x = 220;
    height = 20;
    UILabel * womanLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, 60, height)];
    womanLabel.text = NSLocalizedString(@"woman", nil);
    womanLabel.font = fontSize;
    [sexView addSubview:womanLabel];
    
    //角色
    x = 10;
    y = 5 + sexView.frame.size.height + sexView.frame.origin.y;
    width = ScreenWidth - x - 5;
    height = ViewHeigth;
    editingRoleView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editingRoleView.backgroundColor = RGBA(248, 248, 248, 1);
    [editingView addSubview:editingRoleView];
    
    x = 10;
    y = 15;
    width = 100;
    height = 20;
    UILabel * editingTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editingTextLabel.text = NSLocalizedString(@"roleId", nil);
    editingTextLabel.font = fontSize;
    [editingRoleView addSubview:editingTextLabel];
    
    x = 100;
    width = ScreenWidth - x;
    editingRoleLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    editingRoleLabel.font = fontSize;
    editingRoleLabel.text = NSLocalizedString(@"general", nil);
    [editingRoleView addSubview:editingRoleLabel];
    
    roleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    roleBtn.frame = CGRectMake(editingRoleView.frame.size.width - 50, 0, 50, 50);
    roleBtn.backgroundColor = RGBA(135, 199, 66, 1);
    [roleBtn addTarget:self action:@selector(roleClick:) forControlEvents:UIControlEventTouchUpInside];
    [editingRoleView addSubview:roleBtn];
    
    UIImageView * roleImgView = [[UIImageView alloc]initWithFrame:CGRectMake(editingRoleView.frame.size.width - 35, 15, 20, 20)];
    UIImage * roleImgage = [UIImage imageNamed:@"36展开向下"];
    roleImgView.image = roleImgage;
    [editingRoleView addSubview:roleImgView];
    
    if ([_communityRole isEqualToString:@"2"]) {
        roleBtn.backgroundColor = RGBA(248, 248, 248, 1);
        roleBtn.enabled = NO;
        editingRoleLabel.text = roleArray[0];
        roleImgView.hidden = YES;
        roleIndex = @"3";
    }
}

#pragma mark ----------点击手机号
-(void)mobileClick
{
    [telePhoneTextField resignFirstResponder];
    mobileBtn.selected = !mobileBtn.selected;
    if (mobileBtn.selected) {
        emailBtn.selected = NO;
        mobileHeaderBtn.hidden = NO;
        emailHeaderLabel.hidden = YES;
        telePhoneTextField.keyboardType = UIKeyboardTypeNumberPad;
        telePhoneTextField.placeholder = NSLocalizedString(@"loginPromt", nil);
        addAddressTelePhoneBtn.hidden = NO;
    }
    else
    {
        emailBtn.selected = YES;
        emailHeaderLabel.hidden = NO;
        mobileHeaderBtn.hidden = YES;
        telePhoneTextField.keyboardType = UIKeyboardTypeDefault;
        telePhoneTextField.placeholder = NSLocalizedString(@"mineEmail", nil);
        addAddressTelePhoneBtn.hidden = YES;
    }
}


#pragma mark ----------点击邮箱
-(void)emailClick
{
    [telePhoneTextField resignFirstResponder];
    emailBtn.selected = !emailBtn.selected;
    if (emailBtn.selected) {
        mobileBtn.selected = NO;
        emailHeaderLabel.hidden = NO;
        mobileHeaderBtn.hidden = YES;
        telePhoneTextField.keyboardType = UIKeyboardTypeDefault;
        telePhoneTextField.placeholder = NSLocalizedString(@"mineEmail", nil);
        addAddressTelePhoneBtn.hidden = YES;
    }
    else
    {
        mobileBtn.selected = YES;
        mobileHeaderBtn.hidden = NO;
        emailHeaderLabel.hidden = YES;
        telePhoneTextField.keyboardType = UIKeyboardTypeNumberPad;
        telePhoneTextField.placeholder = NSLocalizedString(@"loginPromt", nil);
        addAddressTelePhoneBtn.hidden = NO;
    }
}


#pragma mark ---------- 改变地区或者国家
-(void)chooseArea
{
    AreaChooseController * acc = [[AreaChooseController alloc]init];
    [self.navigationController pushViewController:acc animated:YES];
    acc.areaCode = ^(NSString * areaCodeString,NSString * areaString)
    {
        areaCodeIdLabel.text = [NSString stringWithFormat:@"+%@",areaCodeString];
        areaLabel.text = areaString;
    };
}



-(void)setCommunityRole:(NSString *)communityRole
{
    _communityRole = communityRole;
}

-(void)resignClick
{
    [userNameTextField resignFirstResponder];
    [telePhoneTextField resignFirstResponder];
}


-(void)clickPromtView
{
    [promtView removeFromSuperview];
    [userInfo setValue:@"no" forKey:@"address"];
    [userInfo synchronize];
}

#pragma mark -- 从通讯录添加职员
-(void)addTelePhone
{
    ABPeoplePickerNavigationController * peoplePicker = [[ABPeoplePickerNavigationController alloc]init];
    peoplePicker.peoplePickerDelegate = self;
    [self presentViewController:peoplePicker animated:NO completion:nil];
}

-(void)manClick
{
    if (manBtn.selected) {
        manBtn.selected = NO;
        womanBtn.selected = YES;
        sexFlag = @"0";
    }
    else
    {
        manBtn.selected = YES;
        womanBtn.selected = NO;
        sexFlag = @"1";
    }
}

-(void)womanClick
{
    if (womanBtn.selected) {
        manBtn.selected = YES;
        womanBtn.selected = NO;
        sexFlag = @"1";
    }
    else
    {
        manBtn.selected = NO;
        womanBtn.selected = YES;
        sexFlag = @"0";
    }
}

-(void)saveClick
{
    [telePhoneTextField resignFirstResponder];
    [userNameTextField resignFirstResponder];
    if (areaCodeIdLabel.text == nil || [areaCodeIdLabel.text isEqualToString:@""] || [areaCodeIdLabel.text isKindOfClass:[NSNull class]]) {
        [PromptInfoView showMessage:@"errorAreaOrCountry"];
        return;
    }
    if ([validateInputValue validateStringIsNull:telePhoneTextField.text]) {
        [PromptInfoView showMessage:NSLocalizedString(@"errorAccount", nil) superView:self.view];
        return;
    }
    
    if(mobileBtn.selected)
    {
        if (![validateInputValue isPureNumandCharacters:telePhoneTextField.text]) {
            [PromptInfoView showMessage:NSLocalizedString(@"AccountError", nil)];
            return;
        }
    }
    if (emailBtn.selected)
    {
        if (![validateInputValue validateEmail:telePhoneTextField.text]) {
            [PromptInfoView showMessage:NSLocalizedString(@"AccountError", nil)];
            return;
        }
    }
    
    if ([validateInputValue validateStringIsNull:userNameTextField.text]) {
        [PromptInfoView showMessage:NSLocalizedString(@"userNameLength", nil) superView:self.view];
        return;
    }

    NSString * localizString = NSLocalizedString(@"sureAdd", nil);
    NSString * telePhoneString = [NSString stringWithFormat:@"%@%@？",localizString, telePhoneTextField.text];
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:telePhoneString delegate:self cancelButtonTitle:NSLocalizedString(@"alterCancel", nil) otherButtonTitles:NSLocalizedString(@"alterSure", nil), nil];
    [alert show];
}


#pragma mark -- alertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self makeSureSave];
    }
}

-(void)makeSureSave
{
    if ([self IsHaveNetwork]) {
        return;
    }
    userInfo = [NSUserDefaults standardUserDefaults];
    [saveDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [saveDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [saveDict setValue:userNameTextField.text forKey:@"name"];
    [saveDict setValue:sexFlag forKey:@"gender"];
    [saveDict setValue:roleIndex forKey:@"role"];
    
    if([validateInputValue isPureNumandCharacters:telePhoneTextField.text]==YES)
    {
        [saveDict setValue:telePhoneTextField.text forKey:@"mobile"];
        [saveDict setValue:[areaCodeIdLabel.text substringFromIndex:1] forKey:@"areaCode"];
    }
    else if ([validateInputValue validateEmail:telePhoneTextField.text]==YES)
    {
        [saveDict setValue:telePhoneTextField.text forKey:@"email"];
    }
    [saveDict setValue:_communityID forKey:@"communityId"];
    
    if([self getTimestamp:_communityID timeKey:@"member_timestamp"] != nil)
    {
        [saveDict setValue:[self getTimestamp:_communityID timeKey:@"member_timestamp"] forKey:@"timestamp"];
    }
    else
    {
        [saveDict setValue:@"" forKey:@"timestamp"];
    }
    [self startWaiting:nil color:nil addToView:self.view];
    RequestNetWork * rnk = [[RequestNetWork alloc]init];
    [rnk initWithPath:POST(ADDUSER) params:saveDict block:^(id downLoadData, id error) {
        [self stopWaiting];
        NSDictionary * dict = [downLoadData objectForKey:@"data"];
        NSDictionary * cache = [downLoadData objectForKey:@"cache"];
        if (cache != nil) {
            if([@"0" isEqualToString:[NSString stringWithFormat:@"%@",[cache objectForKey:@"status"]]]==YES)
            {
                [self updateTimestamp:_communityID timeKey:@"member_timestamp" timeKeyValue:[cache objectForKey:@"timestamp"]];
            }
            if (dict != nil)
            {
                StaffModel * model = [[StaffModel alloc]init];
                model.userID = [dict objectForKey:@"id"];
                model.headUrl = [dict objectForKey:@"headUrl"];
                model.mobile = [dict objectForKey:@"mobile"];
                model.userName = [dict objectForKey:@"name"];
                model.sex = [dict objectForKey:@"gender"];
                model.email = [dict objectForKey:@"email"];
                model.StaffRole = [NSString stringWithFormat:@"%@",[dict objectForKey:@"role"]];
                model.headerWord=[dict objectForKey:@"firstChar"];
                if([validateInputValue isPureNumandCharacters:telePhoneTextField.text]==YES)
                {
                     model.areaCode = [areaCodeIdLabel.text substringFromIndex:1];
                }
                StaffManagerViewController * svc = [[StaffManagerViewController alloc]init];
                svc.communityId = _communityID;
                svc.role = [_communityRole integerValue];
                svc.MyselfPower = YES;
                svc.model = model;
                StaffLogic * staffDataBase = [[StaffLogic alloc]init];
                [staffDataBase insertStaffData:model withCommuntiyId:_communityID];
                [self.navigationController pushViewController:svc animated:YES];
            }
        }
        else if (error != nil) {
            
                [PromptInfoView showMessage:[error objectForKey:@"info"]superView:self.view];
        }
    }];
    
}


-(void)prepareListView
{
    listTableView =[[UITableView alloc]init];
    listTableView.delegate = self;
    listTableView.dataSource = self;
    listTableView.hidden = YES;
    [editingView addSubview:listTableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (editingPositonView.frame.origin.y + editingPositonView.frame.size.height == listTableView.frame.origin.y) {
        return positionArray.count;
    }
    else if(editingDutyView.frame.origin.y + editingDutyView.frame.size.height == listTableView.frame.origin.y)
    {
        return dutyArray.count;
    }
    else
        return roleArray.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40.0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * string = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    if (editingPositonView.frame.origin.y + editingPositonView.frame.size.height == listTableView.frame.origin.y) {
        if (positionArray.count > indexPath.row) {
            cell.textLabel.text = positionArray[indexPath.row];
            
        }
    }
    else if(editingDutyView.frame.origin.y + editingDutyView.frame.size.height == listTableView.frame.origin.y)
    {
        if (dutyArray.count > indexPath.row) {
            cell.textLabel.text = dutyArray[indexPath.row];
            
        }
    }
    else
    {
        if (roleArray.count > indexPath.row) {
            cell.textLabel.text = roleArray[indexPath.row];
            
        }
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingPositonView.frame.origin.y + editingPositonView.frame.size.height == listTableView.frame.origin.y){
        editingPositonLabel.text = positionArray[indexPath.row];
        positionIndex = [NSString stringWithFormat:@"%d",(int)indexPath.row];
    }
    else if(editingDutyView.frame.origin.y + editingDutyView.frame.size.height == listTableView.frame.origin.y)
    {
        editingDutyLabel.text = dutyArray[indexPath.row];
        dutyIndex = [NSString stringWithFormat:@"%d",(int)indexPath.row];
    }
    else
    {
        editingRoleLabel.text = roleArray[indexPath.row];
        roleIndex = [NSString stringWithFormat:@"%d",3 - (int)indexPath.row];
    }
    listTableView.hidden = YES;
}

#pragma mark -- ABPeoplePickerNavigationControllerDelegate 获取名字和电话
-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
    ABMutableMultiValueRef phoneMult = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString * fisrtName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString * lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    if (fisrtName == nil) {
        fisrtName = @"";
    }
    if (lastName == nil) {
        lastName = @"";
    }
    userNameTextField.text = [NSString stringWithFormat:@"%@%@",lastName,fisrtName];
    
    NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < ABMultiValueGetCount(phoneMult); i ++) {
        NSString * aphone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMult, i);
        [phones addObject:aphone];
    }
    if (phones.count > 0) {
        telePhoneTextField.text = [phones objectAtIndex:0];
        telePhoneTextField.text = [telePhoneTextField.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }
    
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -- ABPeoplePickerNavigationControllerDelegate 取消的方法
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
}

//2_0到8_0系统使用的方法
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    ABMutableMultiValueRef phoneMult = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString * fisrtName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString * lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    if (fisrtName == nil) {
        fisrtName = @"";
    }
    if (lastName == nil) {
        lastName = @"";
    }
    userNameTextField.text = [NSString stringWithFormat:@"%@%@",lastName,fisrtName];
    
    NSMutableArray *phones = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < ABMultiValueGetCount(phoneMult); i ++) {
        NSString * aphone = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phoneMult, i);
        [phones addObject:aphone];
    }
    if (phones.count > 0) {
        telePhoneTextField.text = [phones objectAtIndex:0];
        telePhoneTextField.text = [telePhoneTextField.text stringByReplacingOccurrencesOfString:@"-" withString:@""];
    }
    
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
    return NO;
}


-(void)roleClick:(UIButton *)btn
{
    [self resignClick];//解除第一响应
    float x = editingRoleView.frame.origin.x;
    float y = editingRoleView.frame.size.height + editingRoleView.frame.origin.y;
    float width = [UIScreen mainScreen].bounds.size.width - x - 20;
    float height = 80;
    
    listTableView.frame = CGRectMake(x, y, width, height);
    if (listTableView.hidden) {
        listTableView.hidden = NO;
    }
    else
        listTableView.hidden = YES;
    [listTableView reloadData];
}

#pragma mark --- uitextfieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self resignClick];
    return YES;
}
-(void)hiddenKeyboard
{
    [self resignClick];
    listTableView.hidden = YES;
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(void)addUserInfoViewSendToFlag:(NSString *)string
{
    if ([string isEqualToString:@"room"])
    {
        RoomSeclectViewController * rsvc = [[RoomSeclectViewController alloc]init];
        rsvc.communityid = _communityID;
        rsvc.guestOrStaff = @"1";
        rsvc.memberid = [userInfo objectForKey:@"uid"];
        [self.navigationController pushViewController:rsvc animated:YES];
    }
    else if([string isEqualToString:@"manager"])
    {
        AccessSeclectViewController * asvc = [[AccessSeclectViewController alloc]init];
        asvc.communityid = _communityID;
        asvc.memberid = [userInfo objectForKey:@"uid"];
        [self.navigationController pushViewController:asvc animated:YES];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
